import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';

///**
///* Description: 排序组件
///*/
class FilterSortWidget extends StatefulWidget {
  ///由于item的文字长短不确定,所以添加一个文本大小的可选参数

  FilterSortWidget({@required this.items, this.sortItemPress, this.textSize = 8, this.isEnableDefault = false, this.defaultEnabledIndex = 0, this.defaultEnabledSortType = "desc", this.isEnable = false, this.enabledIndex = 0, this.enabledSortType = "desc", this.isScroll = false});

  final List<BaseSelectModel> items;

  final Function(Map<String, dynamic> sorts) sortItemPress;

  final double textSize;

  /// 是否在初始化时使排序生效
  final bool isEnableDefault;

  /// 默认生效的item索引，默认第一个，isEnableDefault不为true时将忽略
  final int defaultEnabledIndex;

  /// 默认生效的排序方法，默认desc， isEnableDefault不为true时将忽略
  final String defaultEnabledSortType;

  /// 是否接收排序
  final bool isEnable;

  /// 接收排序生效的item索引，默认第一个，isEnable不为true时将忽略
  final int enabledIndex;

  /// 接收排序生效的排序方法，默认desc， isEnable不为true时将忽略
  final String enabledSortType;

  /// 是否左右滚动
  final bool isScroll;

  @override
  _FilterSortWidgetState createState() => _FilterSortWidgetState();
}

class _FilterSortWidgetState extends State<FilterSortWidget> {
  final List<String> _sortListAsc = <String>[];

  final List<String> _sortListDesc = <String>[];

  bool _isEnable = false;

  @override
  void initState() {
    super.initState();
    if (widget.isEnableDefault) {
      if (widget.defaultEnabledSortType.toUpperCase() == "ASC") {
        this._sortListAsc.add(widget.items[widget.defaultEnabledIndex].key);
      } else {
        this._sortListDesc.add(widget.items[widget.defaultEnabledIndex].key);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    this._isEnable = widget.isEnable;
    if (_isEnable) {
      this._sortListAsc.clear();
      this._sortListDesc.clear();
      this._isEnable = false;
      if (widget.enabledSortType.toUpperCase() == "ASC") {
        this._sortListAsc.add(widget.items[widget.enabledIndex].key);
      } else {
        this._sortListDesc.add(widget.items[widget.enabledIndex].key);
      }
    }
    return widget.isScroll
        ? SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              children: widget.items.map((map) {
                return Container(width: (map.value.length * 25).toDouble(), child: _buildSortItem(map));
              }).toList(),
            ),
          )
        : Row(
            children: widget.items.map((map) {
              return Expanded(child: Container(child: _buildSortItem(map)));
            }).toList(),
          );
  }

  Widget _buildSortContainer({Widget child, Function onPress}) {
    return InkWell(
      onTap: () => onPress(),
      child: Container(
        height: 50.0,
        alignment: Alignment.center,
        child: child,
      ),
    );
  }

  Widget _buildSortItem(BaseSelectModel item) {
    return _buildSortContainer(
      onPress: () => _sortIconPress(item),
      child: Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Text(
            item.value,
            style: widget.textSize != null ? TextStyle(fontSize: widget.textSize, color: Color(0xFF333333)) : null,
          ),
          _buildShowSortIcon(item),
        ],
      ),
    );
  }

  _sortIconPress(BaseSelectModel item) {
    if (this._sortListAsc.contains(item.key)) {
      setState(() {
        this._sortListAsc.clear();
        this._sortListDesc.clear();
        this._sortListDesc.add(item.key);
      });
    } else if (this._sortListDesc.contains(item.key)) {
      setState(() {
        this._sortListAsc.clear();
        this._sortListDesc.clear();
        this._sortListAsc.add(item.key);
      });
    } else {
      setState(() {
        this._sortListAsc.clear();
        this._sortListDesc.clear();
        this._sortListDesc.add(item.key);
      });
    }
    widget.sortItemPress({'sortListAsc': _sortListAsc, 'sortListDesc': _sortListDesc});
  }

  Widget _buildShowSortIcon(BaseSelectModel item) {
    return _buildSortIcon(item);
  }

  Widget _buildSortIcon(BaseSelectModel item) {
    var selectedColor = Theme.of(context).primaryColor;
    if (selectedColor == Colors.white) selectedColor = Colors.black;
    Color sortBottomColor = this._sortListDesc.contains(item.key) ? selectedColor : Theme.of(context).iconTheme.color;
    Color sortTopColor = this._sortListAsc.contains(item.key) ? selectedColor : Theme.of(context).iconTheme.color;
    return Container(
      padding: EdgeInsets.only(left: 4.0),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(
            BaseIconData.x3SortTopArrow,
            size: 6.0,
            color: sortTopColor,
          ),
          Icon(
            BaseIconData.x3SortBottomArrow,
            size: 6.0,
            color: sortBottomColor,
          ),
        ],
      ),
    );
  }
}
